﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using BLL;
using Models;
using System.IO;
using Common;

namespace Workflow.Project
{
    public partial class Search : System.Web.UI.Page
    {
        private PJSearchManager objPJSearch = new PJSearchManager();
        public List<ProjectMain> resultList = new List<ProjectMain>();
        public SearchChoose objSearchChoose = new SearchChoose();
        protected void Page_Load(object sender, EventArgs e)
        {

        }

        protected void BtnSubmit_Click(object sender, EventArgs e)
        {
            try
            {
                PJSearch pjSearch = GetPJSearch();
                resultList = objPJSearch.GetMain(pjSearch);
            }
            catch (Exception ex)
            {
                string msg = string.Format("<script>alert('{0}')</script>", ex.Message);
                Page.ClientScript.RegisterStartupScript(Page.GetType(), "alert", msg);
            }

        }

        protected void Export_Click(object sender, EventArgs e)
        {
            List<ProjectMain> exportResult = new List<ProjectMain>();
            try
            {
                PJSearch pjSearch = GetPJSearch();
                exportResult = objPJSearch.GetMain(pjSearch);
                GridView GridView1 = new GridView();
                GridView1.AutoGenerateColumns = false;
                GridView1.Columns.Add(new BoundField() { DataField = "Mold", HeaderText = "模具号" });
                GridView1.Columns.Add(new BoundField() { DataField = "DisplayName", HeaderText = "项目创建人" });
                GridView1.Columns.Add(new BoundField() { DataField = "StrT1EndDate", HeaderText = "T1预估完成时间" });
                GridView1.Columns.Add(new BoundField() { DataField = "StrT2EndDate", HeaderText = "T2预估完成时间" });
                GridView1.Columns.Add(new BoundField() { DataField = "StrT3EndDate", HeaderText = "T3预估完成时间" });
                GridView1.Columns.Add(new BoundField() { DataField = "StrT4EndDate", HeaderText = "T4预估完成时间" });
                GridView1.Columns.Add(new BoundField() { DataField = "StrT5EndDate", HeaderText = "T5预估完成时间" });
                GridView1.Columns.Add(new BoundField() { DataField = "StrT1RealEndDate", HeaderText = "T1完成时间" });
                GridView1.Columns.Add(new BoundField() { DataField = "StrT2RealEndDate", HeaderText = "T2完成时间" });
                GridView1.Columns.Add(new BoundField() { DataField = "StrT3RealEndDate", HeaderText = "T3完成时间" });
                GridView1.Columns.Add(new BoundField() { DataField = "StrT4RealEndDate", HeaderText = "T4完成时间" });
                GridView1.Columns.Add(new BoundField() { DataField = "StrT5RealEndDate", HeaderText = "T5完成时间" });
                GridView1.DataSource = exportResult;
                GridView1.DataBind();
                ExportToExcel(GridView1);
            }
            catch (Exception ex)
            {
                string msg = string.Format("<script>alert('{0}');</script>", ex.Message.ToString());
                Page.ClientScript.RegisterStartupScript(Page.GetType(), "alert", msg);
            }

        }
        private PJSearch GetPJSearch()
        {
            int pjId1 = objPJSearch.GetId(txtNo1.Text.ToString().Trim().ToUpper());
            int pjId2 = -1;
            PJSearch pjSearch = new PJSearch()
            {
                Id1 = pjId1,
                Id2 = pjId2,
                PJId = txtNo1.Text.ToString().Trim().ToUpper(),
                //PJName = txtPJName.Text.ToString().Trim(),
                CreateTime1 = txtCreateTime1.Text.Trim() == "" ? Convert.ToDateTime("1900-01-01") : Convert.ToDateTime(txtCreateTime1.Text.Trim()),
                CreateTime2 = txtCreateTime2.Text.Trim() == "" ? Convert.ToDateTime("1900-01-01") : Convert.ToDateTime(txtCreateTime2.Text.Trim()),
                CreaterName = txtPJCreater.Text.ToString().Trim(),
                PJType = ddlPJType.SelectedValue,
                MoldType = ddlMoldType.SelectedValue
            };
            return pjSearch;
        }
        public void ExportToExcel(GridView GridView)
        {
            string excelname = DateTime.Now.ToString("yyyyMMddhhmm");
            Response.Clear();
            Response.AddHeader("content-disposition", "attachment;filename=  " + excelname + " .xls");
            Response.Charset = "UTF-8";
            Response.ContentEncoding = System.Text.Encoding.GetEncoding("UTF-8");
            Response.ContentType = "application/type.xls";//设置输出文件类型为excel文件      
            StringWriter stringWrite = new StringWriter();
            HtmlTextWriter htmlWrite = new HtmlTextWriter(stringWrite);
            GridView.RenderControl(htmlWrite);
            Response.Write(stringWrite.ToString());
            Response.End();
        }

        protected void ddlSelect_SelectedIndexChanged(object sender, EventArgs e)
        {
            string msgForm = objSearchChoose.Choose(ddlSelect);
            Page.ClientScript.RegisterStartupScript(Page.GetType(), "", msgForm);
        }
    }
}